<?php
class newsController extends Controller
{
    function showIndex()
    {
        $where = " and 1=1";
        $this->putIntoPage("types", $this->modules->NewsType->find());
        if (array_key_exists("type", $this->_post) && $this->_post['type'] != '') {
            $where = " and n.type=" . $this->_post['type'];
            $this->putIntoPage("newsTypeId", $this->_post['type']);

        }
        $pagination_sql = "select n.*,nt.title as typeName from news n left join news_type nt on n.type = nt.newsTypeId where n.status <> 'D' $where order by n.createDate desc";
        $pagination_count_sql = "select count(1) as count from news n left join news_type nt on n.type = nt.newsTypeId where n.status <> 'D' $where ";
        $this->putIntoPage("pagination", $this->modules->News->pagination($pagination_sql, $pagination_count_sql));
        $this->render("NewsList");
    }

    function add()
    {
        $this->putIntoPage("types", $this->modules->NewsType->find());
        $this->render("NewsAdd");
    }

    function edit()
    {
        $this->putIntoPage("news", $this->modules->News->load($this->_get['id']));
        $this->putIntoPage("types", $this->modules->NewsType->find());
        $this->render("NewsAdd");
    }

    function save()
    {
        $this->putIntoPage("isSuccess", isset($this->_post['newsId']) && $this->_post['newsId'] > 0
                                              ? $this->modules->News->update($this->_post)
                                              : $this->modules->News->insert($this->_post));
        $this->showIndex();
    }

    function del()
    {
        $this->modules->News->delete($this->_get['id']);
        $this->showIndex();
    }
}

?>